package ru.housewife.model;

import org.hibernate.annotations.Cascade;

import javax.persistence.*;
import java.util.List;

import static org.hibernate.annotations.CascadeType.ALL;
import static org.hibernate.annotations.CascadeType.DELETE_ORPHAN;

/**
 * Entity for ingredient groups
 *
 * @author atroshanin
 */
@Entity
@Table(name = "INGREDIENTS_GROUPS")
@SequenceGenerator(name = "ingredients_groups", sequenceName = "INGREDIENTS_GROUPS_SEQ")
public class IngredientGroup {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ingredients_groups")
    @Column(name = "ID")
    private Long id;

    @Column(name = "NAME", length = 50)
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PARENT_ID")
    private IngredientGroup parent;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "parent", cascade = CascadeType.ALL)
    @Cascade({ ALL, DELETE_ORPHAN })
    private List<IngredientGroup> childs;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "ingredientGroup", cascade = CascadeType.ALL)
    @Cascade({ ALL, DELETE_ORPHAN })
    private List<Ingredient> ingredients;

    @Column(name = "IS_DEFAULT")
    private boolean defaultGroup;

    @Column(name = "SORT_ORDER")
    private int order;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public IngredientGroup getParent() {
        return parent;
    }

    public void setParent(IngredientGroup parent) {
        this.parent = parent;
    }

    public List<IngredientGroup> getChilds() {
        return childs;
    }

    public void setChilds(List<IngredientGroup> childs) {
        this.childs = childs;
    }

    public List<Ingredient> getIngredients() {
        return ingredients;
    }

    public void setIngredients(List<Ingredient> ingredients) {
        this.ingredients = ingredients;
    }

    public boolean isDefaultGroup() {
        return defaultGroup;
    }

    public void setDefaultGroup(boolean defaultGroup) {
        this.defaultGroup = defaultGroup;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }
}
